﻿using System;
using System.Collections.Generic;
using System.IO;
using Docentric.Examples.Data.BusinessObjects;
using Docentric.Examples.Data.BusinessObjects.DataModel;
using Docentric.Word;


namespace Docentric.Examples.Browser
{
	public class GroupingExample : ExampleBase
	{
		// Constructor
		public GroupingExample()
		{
			Name = "Grouping";
			Title = "Grouping";
			Description = "This example shows how you can use the Group element to render a collection of products grouped by category.";
		}


		// GenerateReport
		public override string GenerateReport()
		{
			// Get all products from the database.
			IEnumerable<Product> products = DataAccess.GetProducts();

			// Create a temporary file for the generated report document.
			string reportDocumentFileName = System.IO.Path.GetTempPath() + "GeneratedReport_" + Guid.NewGuid().ToString() + ".docx";
			using (Stream reportDocumentStream = File.Create(reportDocumentFileName))
			{
				// Open the report template file.
				using (Stream reportTemplateStream = GetReportTemplate())
				{
					// Generate the report document using 'DocumentGenerator'.
					DocumentGenerator dg = new DocumentGenerator(products);
					DocumentGenerationResult result = dg.GenerateDocument(reportTemplateStream, reportDocumentStream);
					if (result.HasErrors)
					{
						foreach (Word.Error error in result.Errors) Console.Out.WriteLine(error.Message);
					}
				}
			}

			return reportDocumentFileName;
		}
	}
}
